package com.maven.front.service.manageCourseKpoint;

//      课程管理模块：
//            对课程的章节操作

import com.maven.front.bean.*;
import com.maven.front.dto.ZTreeVo;
import com.maven.front.mapper.EduCourseKpointMapper;
import com.maven.front.mapper.EduCourseMapper;
import com.maven.front.mapper.EduTeacherMapper;
import com.maven.front.util.FileUploadService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.*;

@Slf4j
@Service
public class EduCourseKpointService {

    @Resource
    private EduCourseKpointMapper eduCourseKpointMapper;
    @Autowired
    private FileUploadService fileUploadService;
    @Resource
    private EduCourseMapper eduCourseMapper;
    @Resource
    private EduTeacherMapper eduTeacherMapper;

    //    根据课程ID查询当前课程的章节数据
    public Result selectEduCourseKpoint(int courseId){
        log.info("课程ID："+courseId);
        Map map = new HashMap();
        EduCourse eduCourse = eduCourseMapper.selectByPrimaryKey(courseId);
        List<EduCourseKpoint> list = eduCourseKpointMapper.selectEduCourse(courseId);
        List<ZTreeVo> listz = new ArrayList<ZTreeVo>();
        if(CollectionUtils.isEmpty(list)){
            log.info("当前课程章节为零！");
        }else{
            for (EduCourseKpoint l:list){
                ZTreeVo zTreeVo = new ZTreeVo();
                zTreeVo.setId(l.getKpointId());
                zTreeVo.setpId(l.getParentId());
                zTreeVo.setName(l.getName());
                listz.add(zTreeVo);
            }
        }
        map.put("listz",listz);
        map.put("eduCourse", eduCourse);
        return new Result(200, "查询成功！", map);
    }

    //    删除章节信息
    public Result deleteByPrimaryKey(int kpointId){
        int num = eduCourseKpointMapper.deleteByPrimaryKey(kpointId);
        if (num > 0){
            return new Result(200,"删除成功！", num);
        } else {
            return new Result(10005, "删除失败！", null);
        }
    }


    //    对章节数据进行修改
    public Result updateEduCourseKpoint(EduCourseKpoint eduCourseKpoint){
        int num = eduCourseKpointMapper.updateByPrimaryKeySelective(eduCourseKpoint);
        if (num > 0){
            return new Result(200, "修改成功！", num);
        } else {
            return new Result(10006, "修改失败！", null);
        }
    }

    //    根据章节ID获取要修改的数据
    public Result getById(Integer kpointId){
        Map map = new HashMap();//用来存查询的章节信息以及当前章节的教师信息
        EduCourseKpoint eduCourseKpoint = eduCourseKpointMapper.selectByPrimaryKey(kpointId);
        EduTeacher eduTeacher = eduTeacherMapper.selectByPrimaryKey(eduCourseKpoint.getTeacherId());
        map.put("eduCourseKpoint", eduCourseKpoint);
        map.put("eduTeacher", eduTeacher);
        if (ObjectUtils.isEmpty(eduCourseKpoint)){
            return new Result(10005, "查询失败！", null);
        } else {
            return new Result(200, "查询成功！", map);
        }
    }

    //      新增章节信息
    public Result saveEduCourseKopint(EduCourseKpoint eduCourseKpoint, MultipartFile file)throws Exception{
        FileUploadResult result = fileUploadService.upload(file);
//          视频地址
        eduCourseKpoint.setVideoUrl(result.getResponse());
        int num = eduCourseKpointMapper.insertSelective(eduCourseKpoint);
        if (num > 0 ) {
            return new Result(200, "新增成功！", num);
        } else {
            return new Result(10006, "新增失败！", null);
        }
    }


//    新增课程节点
    public Result addZtree(Integer courseId, String name, Integer ztreeId){
        EduCourseKpoint eduCourseKpoint = new EduCourseKpoint();
        eduCourseKpoint.setAddTime(new Date());
        eduCourseKpoint.setName(name);
        eduCourseKpoint.setCourseId(courseId);
        eduCourseKpoint.setSort(0);
        eduCourseKpoint.setPlayCount(0);
        eduCourseKpoint.setIsFree(0);
        eduCourseKpoint.setKpointType(0);
        if (ztreeId == -1){//表示新增节点时,未进行选中,添加父节点
            eduCourseKpoint.setParentId(0);
        } else {
            //表示选中了父节点, 为当前父节点新增子节点
            eduCourseKpoint.setParentId(ztreeId);
        }
        log.info(courseId+":--:"+ztreeId+"---:"+name);
        int num = eduCourseKpointMapper.insertSelective(eduCourseKpoint);
        if (num > 0){
            return new Result(200,"节点新增成功！", null);
        } else {
            return new Result(10004, "新增失败！", null);
        }
    }

}
